<template>
    <span>
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('openBiddingRegAdd')"
            size="small"
            v-if="state.signUpState && onlyRelease === false"
            >招标报名</el-button
        >
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('tenderAddendumAdd')"
            size="small"
            v-if="state.addendumState && onlyRelease === false"
            >补遗</el-button
        >
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('bidClosingAdd')"
            size="small"
            v-if="state.closeState && onlyRelease === false"
            >招标关闭</el-button
        >
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('bidEvaluationResultAdd')"
            size="small"
            v-if="state.resultRegisterState && onlyRelease === false"
            >评标结果登记</el-button
        >
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('biddingResultAdd')"
            size="small"
            v-if="state.resultPublicityState && onlyRelease === false"
            >结果公示</el-button
        >
        <el-button
            type="primary"
            class="btn-blue"
            @click="toRouter('bidWinningNoticeAdd')"
            size="small"
            v-if="state.noticeState && onlyRelease === false"
            >中标通知</el-button
        >
        <!-- && !$route.path.includes('otherBidding') -->
        <el-button
            type="primary"
            class="btn-blue"
            size="small"
            v-if="state.applyReleaseState && formData.releaseState !== 1"
            @click="release"
            >发布</el-button
        >
        <el-popover
            placement="top-start"
            width="200"
            trigger="click"
            :content="biddingRegUrl"
            v-if="state.applySignUpState && onlyRelease === false"
        >
            <el-button
                slot="reference"
                type="primary"
                class="btn-blue"
                size="small"
                @click="biddingReg"
                v-if="!$route.path.includes('otherBidding') && !isProcurement"
                >招标报名地址</el-button
            >
        </el-popover>
    </span>
</template>

<script>
import applyRequest from '@/api/tender/apply.js'
import tenderRequest from '@/api/tender/tender.js'
import engineeringOtherRequest from '@/api/tender/engineeringOther.js'
import materialProvisioningRequest from '@/api/tender/materialProvisioning.js'
import materialZhouCaiLeasingRequest from '@/api/tender/materialZhouCaiLeasing.js'
import materialServiceProcurementRequest from '@/api/tender/materialServiceProcurement.js'
import materialMachineIncomeRequest from '@/api/tender/materialMachineIncome.js'
import equipmentProcurementRequest from '@/api/tender/equipmentProcurement.js'
import equipmentLeasingRequest from '@/api/tender/equipmentLeasing.js'

export default {
    props: {
        formData: {
            type: Object,
            default: function () {
                return {}
            }
        },
        //工程施工招标
        api: {
            type: String,
            default: 'default'
        },
        // 是否有发布按钮
        releaseState: {
            type: Boolean,
            default: true
        },
        // 发布类别
        releaseType: {
            type: String,
            default: '1'
        },
        // 只有发布按钮
        onlyRelease: {
            type: Boolean,
            default: false
        }
    },
    data () {
        return {
            state: {
                addendumState: false,
                closeState: false,
                noticeState: false,
                releaseState: false,
                resultPublicityState: false,
                resultRegisterState: false,
                signUpState: false
            },
            biddingRegUrl: ''
        }
    },
    computed: {
        tenderId () {
            return this.$route.query.tenderId || this.$route.query.billid
        },
        isProcurement () {
            if (
                this.$route.path.includes('equipment') ||
                this.$route.path.includes('material')
            ) {
                return true
            } else {
                return false
            }
        }
    },
    created () {
        this.getButtonState()
        setTimeout(() => {
            console.log('formData123', this.formData)
        }, 1000)
    },
    methods: {
        async getButtonState () {
            let res = null
            const params = {
                billId: this.$route.query.billid || this.$route.query.tenderId,
                type: this.releaseType
            }
            if (this.api === 'default') {
                res = await applyRequest.getButtonState(params)
            } else if (this.api === 'engineeringOther') {
                res =
                    await engineeringOtherRequest.engineeringOtherGetButtonState(
                        params
                    )
            } else if (this.api === 'materialProvisioning') {
                res = await materialProvisioningRequest.getButtonState(params)
            } else if (this.api === 'materialZhouCaiLeasing') {
                res = await materialZhouCaiLeasingRequest.getButtonState(
                    params
                )
            } else if (this.api === 'materialServiceProcurement') {
                res = await materialServiceProcurementRequest.getButtonState(
                    params
                )
            } else if (this.api === 'materialMachineIncome') {
                res = await materialMachineIncomeRequest.getButtonState(params)
            } else if (this.api === 'equipmentProcurement') {
                res = await equipmentProcurementRequest.getButtonState(params)
            } else if (this.api === 'equipmentLeasing') {
                res = await equipmentLeasingRequest.getButtonState(params)
            }
            this.state = res
            // 测试全true
            // this.state = {
            //     addendumReleaseState: true,
            //     addendumSignUpState: true,
            //     addendumState: true,
            //     applyReleaseState: true,
            //     applySignUpState: true,
            //     closeState: true,
            //     noticeState: true,
            //     resultPublicityState: true,
            //     resultRegisterState: true
            // }
        },
        toRouter (path) {
            if (this.api !== 'default') {
                // 首字母转大写
                const characters = [...path]
                characters[0] = characters[0].toUpperCase()
                path = characters.join('')
            }
            if (this.api === 'default') {
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'engineeringOther') {
                path = 'other' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'materialProvisioning') {
                path = 'material' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'materialZhouCaiLeasing') {
                path = 'hire' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'materialServiceProcurement') {
                path = 'service' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'materialMachineIncome') {
                path = 'machine' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'equipmentProcurement') {
                path = 'equipment' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            } else if (this.api === 'equipmentLeasing') {
                path = 'equipHire' + path
                this.$router.push({
                    path,
                    query: { tenderId: this.tenderId }
                })
            }
        },
        // 发布
        release () {
            const callback = () => {
                var params = { billId: this.tenderId, type: this.releaseType }
                // 工程
                if (this.api === 'default') {
                    tenderRequest.tenderRelease(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                        this.$emit('getData')
                    })
                    // 工程其他
                } else if (this.api === 'engineeringOther') {
                    engineeringOtherRequest
                        .engineeringOtherRelease(params)
                        .then(() => {
                            this.clientPop('suc', '发布成功')
                            this.getButtonState()
                        })
                    // 物资供应
                } else if (this.api === 'materialProvisioning') {
                    console.log('this.api', this.api)
                    materialProvisioningRequest.release(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                    })
                    // 周材租赁
                } else if (this.api === 'materialZhouCaiLeasing') {
                    materialZhouCaiLeasingRequest.release(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                    })
                    // 服务采购
                } else if (this.api === 'materialServiceProcurement') {
                    materialServiceProcurementRequest
                        .release(params)
                        .then(() => {
                            this.clientPop('suc', '发布成功')
                            this.getButtonState()
                        })
                    // 机材收入
                } else if (this.api === 'materialMachineIncome') {
                    materialMachineIncomeRequest.release(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                    })
                    // 设备采购
                } else if (this.api === 'equipmentProcurement') {
                    equipmentProcurementRequest.release(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                    })
                    // 设备租赁
                } else if (this.api === 'equipmentLeasing') {
                    equipmentLeasingRequest.release(params).then(() => {
                        this.clientPop('suc', '发布成功')
                        this.getButtonState()
                    })
                }
            }
            this.clientPop(
                'info',
                '该公示内容会发布到四川路桥官网，且无法撤回，请确认内容是否正确，谨慎操作！',
                callback
            )
        },
        biddingReg () {
            // this.$router.push({
            //     path: 'openBiddingRegAdd',
            //     query: { tenderId: this.tenderId }
            // })
            if (this.api === 'default') {
                this.biddingRegUrl = `${window.location.origin}/openBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'engineeringOther') {
                this.biddingRegUrl = `${window.location.origin}/otherBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'materialProvisioning') {
                this.biddingRegUrl = `${window.location.origin}/materialBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'materialZhouCaiLeasing') {
                this.biddingRegUrl = `${window.location.origin}/hireBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'materialServiceProcurement') {
                this.biddingRegUrl = `${window.location.origin}/serviceBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'materialMachineIncome') {
                this.biddingRegUrl = `${window.location.origin}/machineBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'equipmentProcurement') {
                this.biddingRegUrl = `${window.location.origin}/equipmentBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            } else if (this.api === 'equipmentLeasing') {
                this.biddingRegUrl = `${window.location.origin}/equipHireBiddingOpenBiddingRegAdd?tenderId=${this.tenderId}`
            }
        }
    }
}
</script>

<style lang="sass" scoped></style>
